package com.zhangxiaofanfan.hotcode.second;

import java.util.Arrays;

/**
 *
 * @date 2024-07-13 03:14:13
 * @author zhangxiaofanfan
 */
public class HotCode079 {
    public static void main(String[] args) {
        HotCode079 hotCode = new HotCode079();
        int[] nums = {2,3,0,1,4};
        System.out.println(hotCode.jump(nums));
    }

    public int jump(int[] nums) {
        int[] jumpCount = new int[nums.length];
        Arrays.fill(jumpCount, Integer.MAX_VALUE);
        jumpCount[0] = 0;
        for (int i = 0; i < nums.length; i++) {
            int maxLength = i + nums[i];
            for (int j = i + 1; j <= maxLength && j < nums.length; j++) {
                jumpCount[j] = Math.min(jumpCount[j], jumpCount[i] + 1);
            }
            if (jumpCount[jumpCount.length - 1] != Integer.MAX_VALUE) {
                break;
            }
        }
        return jumpCount[jumpCount.length - 1] == Integer.MAX_VALUE ? -1 : jumpCount[jumpCount.length - 1];
    }

}
